import time
import requests
import execjs

#  读取js文件
with open('test.js', 'r', encoding='utf-8') as f:
    js_code = f.read()

code = execjs.compile(js_code)
# print(code)  # <execjs._exceptions.ProgramError: Cannot find module 'js2py'>  # 读取js文件
#  调用js函数
# result = code.call('code_md5')
timestamp = str(int(time.time() * 1000))

t = "asdjnjfenknafdfsdfsd"
d = "fanyideskweb"
u = "webfanyi"

str_code = f"client={d}&mysticTime={timestamp}&product={u}&key={t}"
# print(str_code)
sign = code.call('code_md5', str_code)

# print(timestamp)

# exit()
# print(len("1729430398613"))   13
# print(len(str(int(timestamp))))

# word = input("请输入你想要的单词: ")

headers = {
    "Accept": "application/json, text/plain, */*",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Content-Type": "application/x-www-form-urlencoded",
    "Origin": "https://fanyi.youdao.com",
    "Pragma": "no-cache",
    "Referer": "https://fanyi.youdao.com/",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-site",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
    "sec-ch-ua": "\"Google Chrome\";v=\"129\", \"Not=A?Brand\";v=\"8\", \"Chromium\";v=\"129\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\""
}
cookies = {
    "OUTFOX_SEARCH_USER_ID": "1185679850@39.168.33.82",
    "OUTFOX_SEARCH_USER_ID_NCOO": "545365802.7970444",
    "DICT_DOCTRANS_SESSION_ID": "ZDIxMzFmYzEtZTZiZC00ZmYzLTgyNzktYmFkNmVhMWRjYjE1",
    "_uetsid": "92f018708ee211efbafe1bced33807a6",
    "_uetvid": "97edc0408ba111efaf4ce10686f558db",
    "_uetmsclkid": "_uet5cd56095535a1b33c662079adb751bac"
}

data = {
    "i": "苹果",
    "from": "auto",
    "to": "",
    "useTerm": "false",
    "domain": 0,
    "dictResult": "true",
    "keyid": "webfanyi",
    "sign": sign,
    "client": "fanyideskweb",
    "product": "webfanyi",
    "appVersion": "1.0.0",
    "vendor": "web",
    "pointParam": "client,mysticTime,product",
    "mysticTime": timestamp,
    "keyfrom": "fanyi.web",
    "mid": "1",
    "screen": "1",
    "app": "1",
    "network": "wifi",
    "abtest": "0",
    "yduuid": "abcdefg"
}
print(sign)
# exit()

# url = "https://dict.youdao.com/webtranslate"
url = f"https://dict.youdao.com/webtranslate/key?keyid=webfanyi-key-getter&sign={sign}&client=fanyideskweb&product=webfanyi&appVersion=1.0.0&vendor=web&pointParam=client,mysticTime,product&mysticTime={timestamp}&keyfrom=fanyi.web&mid=1&screen=1&app=1&network=wifi&abtest=0&yduuid=abcdefg"
response = requests.get(url=url, headers=headers, data=data, cookies=cookies)

print(response.text)
# print(response)
